package com.daka.controller;

import com.daka.annotation.Login;
import com.daka.entity.AdministrativeCounty;
import com.daka.entity.AdministrativeRegions;
import com.daka.enumerate.StatusCode;
import com.daka.service.AdministrativeCountyService;
import com.daka.service.AdministrativeRegionsService;
import com.daka.service.OrganizationService;
import com.daka.vo.response.DataResponseVo;
import com.daka.vo.response.org.OrgCountResponseVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;


/**
 * @author beliveli on 2016/3/31 0031.
 */
@Controller
@RequestMapping("/index")
@Slf4j
public class IndexController {

    @Resource
    private AdministrativeCountyService administrativeCountyService;

    @Resource
    private AdministrativeRegionsService administrativeRegionsService;

    @Resource
    private OrganizationService organizationService;

    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/getConfig", consumes = "application/json")
    public DataResponseVo<HashMap<String, Object>> listByOrgId() {

        HashMap<String, Object> resultHash = new HashMap<>();

        List<AdministrativeCounty> countyList = administrativeCountyService.selectAll();
        List<AdministrativeRegions> regionsList = administrativeRegionsService.selectAll();

        resultHash.put("regionList", regionsList);
        resultHash.put("countyList", countyList);
        return new DataResponseVo<>(StatusCode.SUCCESS.getStatusCode(), StatusCode.SUCCESS.getStatusMessage(), resultHash);
    }

    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/count", consumes = "application/json")
    @Login
    public DataResponseVo<OrgCountResponseVo> count(HttpSession session) {
        return organizationService.count(Integer.parseInt(session.getAttribute("orgId").toString()));
    }
}
